package com.mlink.ai.chat.ui.adapter

import android.net.Uri
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.mlink.ai.chat.assistant.robot.databinding.ItemFeedbackAddPhotoBinding
import com.mlink.ai.chat.assistant.robot.databinding.ItemFeedbackPhotoBinding
import com.mlink.ai.chat.utils.MediaUtils

enum class PhotoType {
    PHOTO,
    ADD_PHOTO
}

class FeedbackPhotoRecyclerAdapter : RecyclerView.Adapter<RecyclerView.ViewHolder>() {


    var data = mutableListOf<String>()
    var onSelectPhoto: (() -> Unit)? = null

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return when (viewType) {
            PhotoType.PHOTO.ordinal -> {
                PhotoViewHolder(
                    ItemFeedbackPhotoBinding.inflate(
                        LayoutInflater.from(parent.context),
                        parent,
                        false
                    )
                )
            }

            PhotoType.ADD_PHOTO.ordinal -> {
                AddPhotoViewHolder(
                    ItemFeedbackAddPhotoBinding.inflate(
                        LayoutInflater.from(parent.context),
                        parent,
                        false
                    )
                )
            }

            else -> {
                AddPhotoViewHolder(
                    ItemFeedbackAddPhotoBinding.inflate(
                        LayoutInflater.from(parent.context),
                        parent,
                        false
                    )
                )
            }
        }
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        when (holder) {
            is PhotoViewHolder -> bindPhotoViewHolder(holder, position)
            is AddPhotoViewHolder -> bindAddPhotoViewHolder(holder, position)
        }
    }

    override fun getItemCount(): Int {
        return data.size
    }

    override fun getItemViewType(position: Int): Int {
        return if (data[position] == "#") {
            PhotoType.ADD_PHOTO.ordinal
        } else {
            PhotoType.PHOTO.ordinal
        }
    }

    private fun bindPhotoViewHolder(holder: PhotoViewHolder, position: Int) {
        val uriString = data[position]
        holder.binding.run {
            val uri = Uri.parse(uriString)
            if (MediaUtils.getMimeType(uri, root.context).startsWith("video")) {
                MediaUtils.setVideoThumbnailToImageView(root.context, ivPhoto, uri)
            } else {
                Glide.with(holder.binding.root.context)
                    .load(Uri.parse(uriString))
                    .into(ivPhoto)
            }
            ivDelete.setOnClickListener {
                data.remove(uriString)
                notifyItemRemoved(position)
                notifyItemRangeChanged(position, data.size - position)
            }
        }
    }

    private fun bindAddPhotoViewHolder(holder: AddPhotoViewHolder, position: Int) {
        holder.binding.root.setOnClickListener {
            onSelectPhoto?.invoke()
        }
    }

    inner class PhotoViewHolder(val binding: ItemFeedbackPhotoBinding) :
        RecyclerView.ViewHolder(binding.root)

    inner class AddPhotoViewHolder(val binding: ItemFeedbackAddPhotoBinding) :
        RecyclerView.ViewHolder(binding.root)
}